#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <string>

using namespace std;

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);


}

MainWindow::~MainWindow()
{
    delete ui;
}

/**
 * @brief MainWindow::on_pushButton_clicked
 * 初始化列表
 */
void MainWindow::on_pushButton_clicked()
{
    // 清空
    ui->comboBox->clear();

    for(int i=0;i<20;i++)
    {
        ui->comboBox->addItem(QString::asprintf("item %d",i));
    }
}
/**
 * @brief MainWindow::on_pushButton_2_clicked
 * 清空列表
 */
void MainWindow::on_pushButton_2_clicked()
{
    ui->comboBox->clear();
}

/**
 * @brief MainWindow::on_checkBox_toggled
 * @param checked
 * 是否可以编辑
 */
void MainWindow::on_checkBox_toggled(bool checked)
{

    ui->comboBox->setEditable(checked);


}
/**
 * @brief MainWindow::on_comboBox_textActivated
 * @param arg1
 * 选中一条记录
 */
void MainWindow::on_comboBox_textActivated(const QString &arg1)
{
    qDebug() << arg1 << Qt::endl;
    ui->plainTextEdit->appendPlainText(arg1);
}

void MainWindow::on_pushButton_5_clicked()
{
    ui->comboBox_3->clear();
    QMap<QString,int> city_zone;
    city_zone.insert("北京",10);
    city_zone.insert("上海",20);
    city_zone.insert("广州",30);
    city_zone.insert("深圳",40);

    QMap<QString,int>::const_iterator it = city_zone.constBegin();
    while(it != city_zone.constEnd())
    {
        QString str;
        str.setNum(it.value());
        ui->comboBox_3->addItem(it.key()+"-"+str);
        it++;
    }
}

void MainWindow::on_comboBox_currentTextChanged(const QString &arg1)
{
//    ui->plainTextEdit->appendPlainText(arg1);
}

void MainWindow::on_comboBox_3_textActivated(const QString &arg1)
{
    ui->plainTextEdit->appendPlainText(arg1);
}

void MainWindow::on_pushButton_4_clicked()
{
    ui->plainTextEdit->clear();
}

void MainWindow::on_pushButton_3_clicked()
{
    QTextDocument *doc = ui->plainTextEdit->document();

    int num = doc->blockCount();
    for(int i=0;i<num;i++)
    {
        QTextBlock tb = doc->findBlock(i);
        ui->comboBox->addItem(tb.text());
    }
}

/**
 * @brief MainWindow::on_plainTextEdit_blockCountChanged
 * @param newBlockCount 最新的行数
 *
 * 行内容变化的时候触发
 */
void MainWindow::on_plainTextEdit_blockCountChanged(int newBlockCount)
{
    qDebug() << newBlockCount << Qt::endl;
}

void MainWindow::on_plainTextEdit_selectionChanged()
{

}

void MainWindow::on_checkBox_2_toggled(bool checked)
{
//    ui->plainTextEdit->setEnabled(checked);
    ui->plainTextEdit->setReadOnly(checked);
}

void MainWindow::on_pushButton_6_clicked()
{
    QString str = "hello world";
    std::string s = "hello world!";
    str = str.fromStdString(s);



    ui->lineEdit->setText(str);
}

